<template>
	<div ref="SysRole">
		<el-dialog title="角色信息" width="600px" :visible.sync="dialogFormVisible" :close-on-press-escape="false"
			:close-on-click-modal="false" @closed="afterCloseDialog">
			<el-form :model="sysRoleForm" ref="sysRoleForm" :rules="formRules" label-width="100px">
				<el-form-item label="角色编码" prop="roleCode">
					<el-input v-model="sysRoleForm.roleCode" maxlength="50"></el-input>
				</el-form-item>
				<el-form-item label="角色名称" prop="roleName">
					<el-input v-model="sysRoleForm.roleName" maxlength="50"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="description">
					<el-input type="textarea" v-model="sysRoleForm.description" maxlength="255"></el-input>
				</el-form-item>
				<el-row>
					<el-col :span="16">
						<el-form-item label="权限">
							<el-tree ref="menuTree" :data="menuTreeData" show-checkbox :default-expand-all="true"
								:expand-on-click-node="false" node-key="id" :props="defaultProps"
								@node-click="menuTreeClickNode">
							</el-tree>
						</el-form-item>
					</el-col>
					<el-col :span="8" style="padding: 10px;">
						<div v-for="item of buttons" :key="item.id">
							<el-checkbox v-model="item.checked">{{item.btnName}}</el-checkbox>
						</div>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">返回</el-button>
				<el-button type="primary" @click="saveForm">保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name: "SysRole",
		data() {
			return {
				dialogFormVisible: false,
				sysRoleForm: {
					roleCode: "",
					roleName: "",
					description: ""
				},
				menuTreeData: [],
				menuTreeCheckKeys: [],
				defaultProps: {
					children: "children",
					label: "menuName",
					id: "id"
				},
				buttons: [],
				formRules: {
					roleCode: [{
						required: true,
						message: "请输入角色编码"
					}],
					roleName: [{
						required: true,
						message: "请输入角色名称"
					}]
				}
			};
		},
		methods: {
			initForm(id) {
				this.buttons = [];
				if (id) {
					this.sysRoleForm.id = id;
					this.$api.post('/role/getById', {id:id}).then(response => {
						this.sysRoleForm = response.data;
						this.$api.post('/role/findMenusByRoleId', {roleId:id}).then(response => {
							this.dialogFormVisible = true;
							this.menuTreeData = response.data.menus;
							let roleMenuIds = response.data.roleMenuIds;
							this.$nextTick(() => {
								roleMenuIds.forEach(menuId => {
									this.$refs.menuTree.setChecked(menuId, true, false);
								});
							});
						});
					});
				} else {
					this.$api.post('/role/findMenusByRoleId').then(response => {
						this.dialogFormVisible = true;
						this.menuTreeData = response.data.menus;
						this.menuTreeCheckKeys = [];
					});
				}
			},
			menuTreeClickNode(tree) {
				this.buttons = tree.buttons;
			},
			saveForm() {
				let url ='/role/add';
				if (this.sysRoleForm.id) {
					url = '/role/update';
				}

				this.$refs.sysRoleForm.validate(valid => {
					if (valid) {
						let menuIds = this.$refs.menuTree.getCheckedKeys();
						let halfMenuIds = this.$refs.menuTree.getHalfCheckedKeys();

						if (halfMenuIds && halfMenuIds.length > 0) {
							halfMenuIds.forEach(element => {
								menuIds.push(element);
							});
						}

						this.sysRoleForm.menuIds = menuIds;
						this.sysRoleForm.menus = this.menuTreeData;

						this.$api.post(url, this.sysRoleForm).then(response => {
							if (response.code === "900") {
								this.$message.success(response.message);
								this.dialogFormVisible = false;
								this.$parent.handleSearch();
							} else {
								this.$message.error(response.message);
							}
						});
					}
				});
			},
			afterCloseDialog() {
				this.sysRoleForm = {
					roleCode: "",
					roleName: "",
					description: ""
				};
				this.$refs.sysRoleForm.resetFields();
			}
		}
	};
</script>
